package com.project.dao.impl;

import com.project.bean.UserBean;
import com.project.dao.IUserDao;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Repository
public class UserDaoImpl extends BaseDao implements IUserDao {
    @Override
    public UserBean login(String userName, String pwd) {
        UserBean user = null;
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_user where u_name=? and u_pwd=? ");
            ps.setObject(1, userName);
            ps.setObject(2, pwd);
            rs = ps.executeQuery();
            if (rs.next()) {
                user = getBean();
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return user;
    }

    @Override
    public void addUser(UserBean user) {
        this.setConnection();
        try {
            ps = con.prepareStatement("INSERT INTO t_user(u_name,u_pwd,u_realName,u_level)values (?,?,?,?)");
            ps.setObject(1, user.getName());
            ps.setObject(2, user.getPwd());
            ps.setObject(3, user.getRealName());
            ps.setObject(4, user.getLevel());
            ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            this.close();
        }

    }

    @Override
    public List<UserBean> findAll() {
        List<UserBean> userList = new ArrayList<>();
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_user ");
            rs = ps.executeQuery();
            while (rs.next()) {
                userList.add(getBean());
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
        return userList;
    }

    @Override
    public void delUser(int id) {
        this.setConnection();
        try {
            ps = con.prepareStatement("delete  from t_user where pk_userId=? ");
            ps.setObject(1, id);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
    }

    @Override
    public boolean checkName(String userName) {
        boolean flag = true;
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_user where  u_name=? ");
            ps.setObject(1, userName);
            rs = ps.executeQuery();
            if (rs.next()) {
                flag = false;
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
        return flag;
    }

    @Override
    public UserBean findById(int id) {
        UserBean user = null;
        this.setConnection();
        try {
            ps = con.prepareStatement("select * from t_user where pk_userId=? ");
            ps.setObject(1, id);
            rs = ps.executeQuery();
            if (rs.next()) {
                user = getBean();
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            close();
        }
        return user;
    }

    @Override
    public void updateUserLevel(int id, String level) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update t_user set u_level=? where pk_userId=? ");
            ps.setObject(1, level);
            ps.setObject(2, id);
            ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            this.close();
        }

    }

    @Override
    public void updateUserPwd(int id, String pwd) {
        this.setConnection();
        try {
            ps = con.prepareStatement("update t_user set u_pwd=? where pk_userId=? ");
            ps.setObject(1, pwd);
            ps.setObject(2, id);
            ps.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            this.close();
        }
    }

    public UserBean getBean() throws SQLException {
        UserBean user = new UserBean();
        user.setName(rs.getString("u_name"));
        user.setId(rs.getInt("pk_userId"));
        user.setPwd(rs.getString("u_pwd"));
        user.setRealName(rs.getString("u_realName"));
        user.setLevel(rs.getString("u_level"));
        return user;
    }

}
